function strrpos(haystack, needle, offset){
    var i = (haystack+'').lastIndexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

//Formulaire
$(document).ready(function(){
	$('#form').attr('onsubmit', 'return false');

	$('#sendForm').click(function(){
		sendForm();
	});
	$('#form').submit(function(){
		sendForm();
	})
});

function sendForm(){
	$('#form').slideUp();
	$('#result').slideUp();
	$('#chargement').slideDown();

	var url = $('#form').attr('action');
	if(url == ""){
		var thisUrl = window.location.toString();
		var pos = strrpos(thisUrl, '/')+1;
		url = thisUrl.substr(pos, thisUrl.length-pos)
	}
	var data = "ajax=1&envoyer=1"
	$(':input').each(function(i){
		if(this.type == "radio"){
			if(this.checked) data += "&"+this.name+"="+this.value;
		}else if(this.name != ""){
			data += "&"+this.name+"="+this.value;
		}
	});

	$.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType: 'json',
		success: function(rep){
			$('#result').html(rep['msg']);
			$('#chargement').slideUp();
			$('#result').slideDown();
			if(rep['err']){ $('#form').slideDown(); }
			if(rep['reload'] > 0){ setTimeout('location.reload()', rep['reload']*1000); }
		},
		error: function(msg){
			$('#result').html('<div class="erreur">Erreur...</div>');
			$('#chargement').slideUp();
			$('#result').slideDown();
		}
	});
}